package prettypenny;

import java.util.*;
import java.sql.*;
import javax.swing.table.AbstractTableModel;

public class CarList  extends AbstractTableModel {
    private ArrayList<Car> cars; 
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    private int position;
    private String[] columnNames = {"Car ID", "Make", "Model", "Reg", "Miles", "Fuel", "Notes"};
    private BookingList bookings;
     /*
     * Table
     */
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return cars.size();
    }
    public String getColumnName(int col) {
        return columnNames[col];
    }
     public Booking FindBooking(int car) {
        for(int i=0; i<bookings.getSize(); i++) {
            if(bookings.getPosition(i).getCarID() == car) {
                return bookings.getPosition(i);
            } 
        }
        return null;
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
       switch (columnIndex) {
                case 0:
                    return cars.get(rowIndex).getID();
                case 1:
                    return cars.get(rowIndex).getMake();
                case 2:
                    return cars.get(rowIndex).getModel();
                case 3:
                    return cars.get(rowIndex).getReg();
                case 4:
                    for(int i = 0; i<bookings.getSize();i++) {
                        if(bookings.getPosition(i).getCarID() == cars.get(rowIndex).getID()) {
                            return bookings.getPosition(i).getMileage();
                        }
                    }
                case 5:
                    return cars.get(rowIndex).getFuel();
                case 6:
                    for(int i = 0; i<bookings.getSize();i++) {
                        if(bookings.getPosition(i).getCarID() == cars.get(rowIndex).getID()) {
                            return bookings.getPosition(i).getServiceDesc();
                        }
                    }
            }
        return null;
    }


    
    public CarList(BookingList bookings) {
        this.bookings = bookings;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // server:port:database
            con = DriverManager.getConnection("jdbc:odbc:uni");
            stmt = con.createStatement();
                    // Cars
            cars = new ArrayList<Car>();
            rs = stmt.executeQuery("SELECT * FROM car");
            while (rs.next()) {
                cars.add(new Car(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getBoolean(6)));
            }
        }catch(Exception ex) {
            System.out.println("carlist" + ex.toString());
            
        }
    }

    public ArrayList getClients() {
        return this.cars;
    }

    public Car getFirst() {
        position = 0;
        return cars.get(position);
    }

    public Car getLast() {
        position = cars.size() - 1;
        return cars.get(position);
    }

    public Car getNext() {
        if (position < cars.size()) {
            position++;
        }
        return cars.get(position);
    }

    public Car getPosition(int value) {
        return cars.get(value-1);
    }

    public Car getPrevious() {
        if (position > 0) {
            position--;
        }
        return cars.get(position);
    }

    public int getCarSize() {
        return cars.size() - 1;
    }
}
